package transaction.program;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;
import java.util.List;

public class StudentJDBCTemplate implements StudentDAO{
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;
    private PlatformTransactionManager transactionManager;

    public void setJdbcTemplateObject(JdbcTemplate jdbcTemplateObject) {
        this.jdbcTemplateObject = jdbcTemplateObject;
    }

    public void setTransactionManager(PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Override
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
    }

    @Override
    public void create(String name, Integer age, Integer marks, Integer year) {
        // 事务定义 可设置事务隔离级别，传播行为，是否只读，事务超时
        final DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        definition.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);//隔离级别
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);//传播行为
        definition.setReadOnly(false);//是否只读
        definition.setTimeout(5000);//事务超时
        // 事务状态
        final TransactionStatus status = transactionManager.getTransaction(definition);
        status.isCompleted();
        //status.setRollbackOnly();
        status.isRollbackOnly();// 该方法返回该事务是否已标记为 rollback-only。
        status.isNewTransaction();
        try {
            // 1.插入一条记录
            String SQL1 = "insert into Student (name, age) values (?, ?)";
            jdbcTemplateObject.update( SQL1, name, age);
            // 2.得到插入的记录id
            String SQL2 = "select max(id) from Student";
            int sid = jdbcTemplateObject.queryForObject( SQL2,null,Integer.class );
            // 3.根据id插入另一个表
            String SQL3 = "insert into Marks(sid, marks, year) " +
                    "values (?, ?, ?)";

            jdbcTemplateObject.update( SQL3, sid, marks, year);
            System.out.println("Created Name = " + name + ", Age = " + age);
            // 事务提交
            transactionManager.commit(status);
        }catch (DataAccessException e){
            System.out.println("Error in creating record, rolling back");
            // 事务回滚
            transactionManager.rollback(status);
            throw e;
        }
    }

    @Override
    public List<StudentMarks> listStudents() {
        String SQL = "select * from Student, Marks where Student.id=Marks.sid";
        List <StudentMarks> studentMarks = jdbcTemplateObject.query(SQL,
                new StudentMarksMapper());
        return studentMarks;
    }
}
